package dao;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

import dto.Sentence;
import dto.THPair;

public class XMLLoader {
	public List<THPair> load(String filename) {

		SAXBuilder builder = new SAXBuilder();
		File xmlFile = new File(filename);

		List<THPair> result = null;
		try {

			Document document = builder.build(xmlFile);
			Element rootNode = document.getRootElement();
			List<Element> list = rootNode.getChildren("pair");
			result = new ArrayList<THPair>(list.size());

			for (Element node : list) {
				String entailment = node.getAttributeValue("entailment");
				String t = node.getChildText("t");
				String h = node.getChildText("h");
				
				Sentence text = new Sentence();
				text.setText(t);
				
				Sentence hyp = new Sentence();
				hyp.setText(h);
				
				THPair pair = new THPair();
				pair.setText(text);
				pair.setHyp(hyp);
				pair.setEntailment(entailment);
				result.add(pair);
			}

		} catch (IOException io) {
			System.out.println(io.getMessage());
		} catch (JDOMException jdomex) {
			System.out.println(jdomex.getMessage());
		}
		
		return result;
	}
}
